//
//  SHNewfeatureViewCell.swift
//  SHWeibo
//
//  Created by jingming wang on 16/7/26.
//  Copyright © 2016年 cpy.name. All rights reserved.
//

import UIKit

class SHNewfeatureViewCell: UICollectionViewCell {
    
    var closure:(()->())?
    
    //MARK: - 懒加载控件
    private lazy var bgImageView: UIImageView = UIImageView()
    
    private lazy var startButton = UIButton()
    
    // 创建一个属性,控制按钮的 显示/隐藏
    var index: Int = 0 {
        didSet {
            bgImageView.image = UIImage(named: "new_feature_\(index + 1)")
            
            startButton.hidden = true
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: - 设置界面
    private func setupUI() {
        backgroundColor = RandomColor()
        
        startButton = UIButton(
            title: "立即体验",
            titleColor: UIColor.whiteColor(),
            font: 18,
            backgroundImgName: "new_feature_finish_button",
            isSizeToFit: true,
            target: self,
            action: "didClickButton")

        
        contentView.addSubview(bgImageView)
        contentView.addSubview(startButton)
        
        // 默认开始按钮全部隐藏
        startButton.hidden = true
    }
    //MARK: - 设置布局
    override func layoutSubviews() {
        super.layoutSubviews()
        
        bgImageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        }
        
        startButton.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(contentView)
            make.bottom.equalTo(contentView).offset(-100)
        }
    }
    
    //MARK: - 开始体验按钮监听
    func didClickButton() {
        closure?()
    }
    
    //MARK: - 开始按钮动画
    func startAnimating() {
        startButton.hidden = false
        self.startButton.transform = CGAffineTransformMakeScale(0, 0)
        
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 0, options: [], animations: { () -> Void in
            self.startButton.transform = CGAffineTransformIdentity
            }, completion: nil)
    }


}
